package com.jh.web.ipcam;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.jh.pojo.Entity;
import com.jh.pojo.ipcam.ISPBean;
import com.jh.utils.BlockingQueueUtils;
import com.jh.utils.PROTO;

/**
 * Servlet implementation class ISPServlet
 */
@WebServlet("/ISPServlet")
public class ISPServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static final Logger logger = LoggerFactory.getLogger(ISPServlet.class);

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ISPServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset:utf-8");
        String ipcamIP = request.getParameter("ipcamIP");
        String brighValue = request.getParameter("brighValue");
        String contrValue = request.getParameter("contrValue");
        String saturValue = request.getParameter("saturValue");
        String hueValue = request.getParameter("hueValue");

        System.out.println("ipcamIP: " + ipcamIP + " brighValue: " + brighValue + " contrValue: " + contrValue
                + " saturValue: " + saturValue + " hueValue: " + hueValue);
        logger.debug("--->" + "ipcamIP: " + ipcamIP + " brighValue: " + brighValue + " contrValue: " + contrValue
                + " saturValue: " + saturValue + " hueValue: " + hueValue);

        BlockingQueueUtils queueUtils = BlockingQueueUtils.getInstance();

        ArrayBlockingQueue<String> txBq = queueUtils.gettxBq();
        Gson gson = new Gson();
        ISPBean isp = new ISPBean();
        Entity entity = new Entity();
        isp.setIp(ipcamIP);
        isp.setBrighVal(Integer.parseInt(brighValue));
        isp.setContrVal(Integer.parseInt(contrValue));
        isp.setSaturVal(Integer.parseInt(saturValue));
        isp.setHueVal(Integer.parseInt(hueValue));
        entity.setType(PROTO.TYPE.ISP);
        entity.setData(isp);
        String json = gson.toJson(entity);
        logger.debug("--->" + " ISP BRIGHT json: " + json);
        try {
            txBq.put(json);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
